Skip to content

[CDRIVER-5996] Prevent multi-evaluation of arithmetic macro-function operands #1999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vector-of-bool
Copy link
Contributor

Currently, mlib_upsize_integer uses the expression (0 & V) to obtain a zero-value for the expression V. This has the unfortunate effect that it must evaluate V in order to obtain this value.

There's a better way: 1 ? 0 : V, which will follow the same integer promotion rules as the infix &, but does not evaluate the right-hand operand. This now allows mlib_upsize_integer and all function-macros based on it to be used safely with operands that are expensive or produce side effects.

The prior defn of `mlib_upsize_integer` would necessarily
double-evaluate the operand expression when it was of
certain types. This new definition makes use of the
ternary operator to obtain an appropriate zero
value without evaluating the operand.
Copy link
Collaborator

@kevinAlbs kevinAlbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat, LGTM!

@kevinAlbs kevinAlbs changed the title Prevent multi-evaluation of arithmetic macro-function operands [CDRIVER-5996] Prevent multi-evaluation of arithmetic macro-function operands Apr 25, 2025
Copy link
Collaborator

@eramongodb eramongodb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I am in favor of the improvement to avoiding (re)evaluation of the argument, I do not observe it addressing -Wtype-limit warnings as suggested with GCC 13.3.0:

warning: comparison of unsigned expression in ‘< 0’ is always false [-Wtype-limits]
      |    ((sizeof ((Value)) < sizeof (intmax_t) || (_mlibGetZero(Value) - 1) < _mlibGetZero(Value)) \
      |                                                                        ^

@eramongodb
Copy link
Collaborator

It looks there may also be a GCC 14 regression where the -Wtype-limits warning is no longer emitted for the same code. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants